856A - Set Theory - CodeForces Solution


brute force constructive algorithms *1600

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#include <iostream>
#include <string>
using namespace std;
#define ll long long
//for math u can observe something related to gcd or something related to even or odd to draw some conclusions
//if your answer is lieing in a range then binary search is the best approach to solve that problem
//for errors check out of bound and overflow of integers
//sorting,binary search,dynamic programming,greedy are some of the most used techniques to solve the problems
//until some element it is good and then next it is bad u should use binary search 
//to find the first good element or to find the first bad element we use binary search
//if there are digits then remember that the digits are just from 0-9 so u can use two loops the same for character a-z they are just 26
const int M=998244353;
const int INF=1e9+5;
void dfs(int currentNode,vector<vector<int>>& adj,int parent,vector<int>& ans)
{
    ans.push_back(currentNode);
    for(int neighbour : adj[currentNode])
    {
        if(neighbour!=parent)
        {
            dfs(neighbour,adj,currentNode,ans);
        }
    }
}
ll mod(ll x)
{
    return (((x%M)+M)%M);
}
ll add(ll a,ll b)
{
    return mod(mod(a)+mod(b));
}
ll mul(ll a,ll b)
{
    return mod(mod(a)*mod(b));
}
int sumd(ll a)
{
    int res=0;
    while(a!=0)
    {
        res+=a%10;
        a/=10;
    }
    return res;
}
int main() {
	// your code goes here
	int t;
    cin>>t;
	while(t--)
	{
	  int n;
	  cin>>n;
	  int a[n];
	  for(int i=0;i<n;i++)
	  {
	      cin>>a[i];
	  }
	  if(n==1)
	  {
	      cout<<"YES"<<endl;
	      cout<<1<<endl;
	  }
	  else
	  {
	      cout<<"YES"<<endl;
	   int b[n];
	   b[0]=1;
	   vector<int>diff(1000005,0);
	   for(int i=0;i<n;i++)
	   {
	     int x=abs(a[i]-1);
	     diff[x]=1;
	   }
	   int i=1;
	   int k=2;
	   while(i<n)
	   {
	       bool ok=true;
	       int p=0;
	       while(p==0)
	       {
	         for(int j=0;j<n;j++)
	         {
	             int x=abs(a[j]-k);
	             if(diff[x]==1)
	             {
	                 p=0;
	                 break;
	             }
	             else
	             {
	                 p=1;
	             }
	         }
	         if(p==0) k++;
	         else
	         {
	             b[i]=k;
	             for(int j=0;j<n;j++)
	             {
	                 int x=abs(a[j]-k);
	                 diff[x]=1;
	             }
	             
	             k++;
	             p=1;
	         }
	       }
	       i++;
	   }
	   for(int z=0;z<n;z++)
	   {
	       cout<<b[z]<<" ";
	   }
	   cout<<endl;
	  }
	  
	  }
	

	return 0;

}


Comments

Submit
0 Comments
More Questions

1455B - Jumps
1225C - p-binary
1525D - Armchairs
1257A - Two Rival Students
1415A - Prison Break
1271A - Suits
259B - Little Elephant and Magic Square
1389A - LCM Problem
778A - String Game
1382A - Common Subsequence
1512D - Corrupted Array
667B - Coat of Anticubism
284B - Cows and Poker Game
1666D - Deletive Editing
1433D - Districts Connection
2B - The least round way
1324A - Yet Another Tetris Problem
246B - Increase and Decrease
22E - Scheme
1566A - Median Maximization
1278A - Shuffle Hashing
1666F - Fancy Stack
1354A - Alarm Clock
1543B - Customising the Track
1337A - Ichihime and Triangle
1366A - Shovels and Swords
919A - Supermarket
630C - Lucky Numbers
1208B - Uniqueness
1384A - Common Prefixes